package com.guodi.model.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guodi.core.minio.util.MinioUtil;
import com.guodi.model.entity.IdtReportFile;
import com.guodi.model.mapper.IdtReportFileMapper;
import com.guodi.model.service.IIdtReportFileService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @描述：上报值关联附件表 服务类
 * @作者: 代鹏刚
 * @日期: 2020-11-07
 */
@Service
public class IdtReportFileServiceImpl extends ServiceImpl<IdtReportFileMapper, IdtReportFile> implements IIdtReportFileService {

    @Resource
    private IdtReportFileMapper idtReportFileMapper;

    @Value("${minio.bucketName}")
    private String bucketName;

    @Override
    public void deleteByReportId(String reportId) {
        QueryWrapper<IdtReportFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("report_id", reportId);
        List<IdtReportFile> idtReportFiles = this.list(queryWrapper);
        // 删除附件
        for (IdtReportFile idtReportFile : idtReportFiles) {
            if (StringUtils.isNotBlank(idtReportFile.getFilePath())) {
//                String group = idtReportFile.getFilePath().substring(0, idtReportFile.getFilePath().indexOf("/"));
//                FastDFSUtil.delete(group, idtReportFile.getFilePath());
                MinioUtil.delete(bucketName,idtReportFile.getFilePath());
            }
        }
        // 删除表数据
        List<String> ids = idtReportFiles.stream().map(x->x.getId()).collect(Collectors.toList());
        this.removeByIds(ids);
    }

    @Override
    public void updateByReportId(String reportId, String targetId) {
        idtReportFileMapper.updateByReportId(reportId, targetId);
    }
}